package com.whfc.ms.api.emp.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppFenceDTO;
import com.whfc.emp.dto.AppFenceEmpDTO;
import com.whfc.emp.param.AppFenceAddParam;
import com.whfc.emp.param.AppFenceEmpAddParam;
import com.whfc.emp.param.AppFenceEmpDelParam;
import com.whfc.emp.param.AppFenceListParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppFenceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 人员管理-电子围栏
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年8月29日
 */
@RequiresPermissions("emp/fence")
@RestController
@RequestMapping("/ms/api/fence")
public class AppFenceController extends BaseController {

    @Autowired
    private MsAppFenceService msAppFenceService;

    /**
     * 人员管理-电子围栏列表
     *
     * @param user
     * @param request
     * @return
     */
    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated AppFenceListParam request) {
        logger.info("人员管理-电子围栏列表,loginer:{},AppFenceListParam:{}", user.getUsername(), request.toString());
        ListData<AppFenceDTO> pageData = msAppFenceService.list(request);
        return ResultUtil.success(pageData);
    }


    /**
     * 人员管理-添加电子围栏
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppFenceAddParam request) {
        logger.info("人员管理-添加电子围栏，loginer:{},AppFenceAddParam:{}", user.getUsername(), request.toString());
        msAppFenceService.add(request);
        return ResultUtil.success();
    }


    /**
     * 人员管理-删除电子围栏
     *
     * @param user
     * @param id
     * @return
     */
    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("id") Integer id) {
        logger.info("人员管理-删除电子围栏,loginer:{},id:{}", user.getUsername(), id);
        msAppFenceService.del(id);
        return ResultUtil.success();
    }

    /**
     * 电子围栏考勤人员列表
     *
     * @param user
     * @param fenceId
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/emp/list")
    public Result empList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("fenceId") Integer fenceId,
                          @RequestParam("pageNum") Integer pageNum,
                          @RequestParam("pageSize") Integer pageSize,
                          @RequestParam(required = false, value = "groupId") Integer groupId,
                          @RequestParam(required = false, value = "workTypeId") Integer workTypeId,
                          @RequestParam(required = false, value = "keyword") String keyword) {
        logger.info("电子围栏考勤人员列表，loginer:{},fenceId:{},pageNum:{},pageSize:{},groupId:{},workTypeId:{},keyword:{}",
                user.getUsername(), fenceId, pageNum, pageSize, groupId, workTypeId, keyword);
        PageData<AppFenceEmpDTO> pageData = msAppFenceService.empList(fenceId, pageNum, pageSize, groupId, workTypeId, keyword);
        return ResultUtil.success(pageData);
    }


    @GetMapping("/emp/unGrantList")
    public Result unGrantList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("deptId") Integer deptId,
                              @RequestParam("fenceId") Integer fenceId,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize") Integer pageSize,
                              @RequestParam(required = false, value = "groupId") Integer groupId,
                              @RequestParam(required = false, value = "keyword") String keyword) {
        logger.info("电子围栏考勤未授权人员列表，loginer:{},deptId:{},fenceId:{},pageNum:{},pageSize:{},groupId:{},keyword:{}",
                user.getUsername(), deptId, fenceId, pageNum, pageSize, groupId, keyword);
        PageData<AppFenceEmpDTO> pageData = msAppFenceService.unGrantList(deptId, fenceId, pageNum, pageSize, groupId, keyword);
        return ResultUtil.success(pageData);
    }

    /**
     * 电子围栏添加人员
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/fence/add")
    @PostMapping("/emp/add")
    public Result addEmp(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated AppFenceEmpAddParam request) {
        logger.info("电子围栏添加人员,loginer:{},param:{}", user.getUsername(), request.toString());
        msAppFenceService.addEmp(request);
        return ResultUtil.success();
    }

    /**
     * 电子围栏删除考勤人员
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/fence/delete")
    @PostMapping("/emp/del")
    public Result delEmp(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated AppFenceEmpDelParam request) {
        logger.info("电子围栏删除考勤人员，loginer:{},param:{}", user.getUsername(), request.toString());
        msAppFenceService.delEmp(request);
        return ResultUtil.success();
    }


    @GetMapping("/listByEmpId")
    public Result listByEmpId(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("empId") Integer empId) {
        logger.info("根据人员id查询所在的电子围栏列表,loginer:{},empId:{}", user.getUsername(), empId);
        ListData<AppFenceDTO> listData = msAppFenceService.listByEmpId(empId);
        return ResultUtil.success(listData);
    }


}
